package net.pec.geotools_svg.svg_conversion;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.opengis.feature.simple.SimpleFeature;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;

public class GenerateMapSymbolElement {

	public Element createSymbol(SimpleFeature feature, Document doc,  Double Yaxismidpoint, 
			double opacity,	int Rotation, int imageWidthperc, int imageHeightperc, 
			String imageURL, String clickEvent, int  pixelHeight, int pixelWidth,
			 Double viewboxWidth, Double viewboxHeight){
		
		
		String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
		Element symbolElement = doc.createElementNS(svgNS, "image");		
			if (clickEvent !=null){
				  symbolElement.setAttributeNS(null, "onclick", clickEvent);
				  }
		symbolElement.setAttributeNS(null,  "id", feature.getID());
		symbolElement.setAttributeNS(null, "opacity", Double.toString(opacity));
		
		Coordinate[] coord;
		Coordinate oneCrd = new Coordinate();		
		Geometry geom = (Geometry) feature.getDefaultGeometry();
		String pointValuey ="";
		String pointValuex ="";
		 
		oneCrd = geom.getCoordinate();
		double coorddiff = oneCrd.y - Yaxismidpoint;
		double newcoord = Yaxismidpoint-coorddiff;
		double offSetX=oneCrd.x-getOffset(viewboxWidth, imageWidthperc);
		double offSetY = newcoord-getOffset(viewboxHeight, imageHeightperc);
		
			  	//pointValues = Double.toString(oneCrd.x) ;
			  	//pointValuex =   centrexCoordinate(oneCrd.x, viewboxWidth, viewboxHeight, imageWidthperc);
			  	symbolElement.setAttributeNS(null, "x", Double.toString(offSetX)); 
			  	//pointValues = Double.toString(newcoord) ;
			  	//pointValuey =   centreyCoordinate(newcoord, viewboxHeight, viewboxWidth, imageHeightperc);
			  	symbolElement.setAttributeNS(null, "y", Double.toString(offSetY)); 
			  	
			  	//symbolElement.setAttributeNS(null,"width",String.valueOf(imageWidth));
			  	//symbolElement.setAttributeNS(null,"height",String.valueOf(imageHeight));
			  	symbolElement.setAttributeNS(null,"width",imageWidthperc+"%");
			  	symbolElement.setAttributeNS(null,"height",imageHeightperc+"%");
			  	symbolElement.setAttributeNS(null,"transform","rotate("+Rotation+","+oneCrd.x+","+newcoord+")");
			  	//symbolElement.setAttributeNS(null,"transform","rotate(matrix(0,1,-1,0,0,0)");
		
		
		
		String strURL = imageURL;
		//imageElement.setAttributeNS(xlinkNS,"xlink:href",myImageUrl);
		symbolElement.setAttributeNS("http://www.w3.org/1999/xlink","xlink:href",strURL);
		
		return symbolElement;
		//gElement.appendChild(symbolElement);
	}
	
	private Double getOffset(Double widthHeight, int imageWidthperc){		
		Double offset = widthHeight*((double)imageWidthperc/100);
		offset=offset/2;
		return offset;
	}
			
	
	
}
